/**
 * Copyright (c) 2018, Alex. All rights reserved.
 */
package com.oschina.bottle.convert;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 转换工具类入口
 *
 * @author <a href="mailto:zhongchao@gegejia.com">zhong</a>
 * @version 1.0 2018/1/20
 * @since 1.0
 */
public class Convert {

    private final static ConverterRegistry REGISTRY = new ConverterRegistry();

    public static void registry(Converter<?, ?> converter) {
        REGISTRY.registry(converter);
    }

    public static  <T> T convert(Object source, Class<T> targetClass) {
        if(source == null)
            return null;

        return REGISTRY.convert(source, targetClass);
    }

    public static <S, T> List<T> convert(List<S> source, Class<T> targetClass) {
        if(source == null)
            return null;

        return source.stream().filter(Objects::nonNull).map(e -> REGISTRY.convert(e, targetClass)).collect(Collectors.toList());
    }
}
